# -*- coding: UTF-8 -*-
from . import BaseController
from settings import logger
from backend.libs.args_util import *
from backend.models import VideoOrder, Team
from backend.libs import myjson as json
from django.db.models import Q


class VideoOrderListController(BaseController):

    def get(self):
        """视频订单列表
        """
        args = PageArgs(self, 'get', 'field', 'fieldValue', 'startTime', 'endTime')
        args.int_arg('state', 0)
        vo = VideoOrder.objects
        if int(args.state) > 0:
            if int(args.state) == 3:
                vo = vo.filter(order_status__in=[3,4,5,6,7])
            else:
                vo = vo.filter(order_status=args.state)
        else:
            vo = vo.filter(~Q(order_status=99))

        if args.field == 'order_id':
            vo = vo.filter(id=args.fieldValue)
        elif args.field == 'city':
            vo = vo.filter(city=args.fieldValue)

        vo = vo.select_related('user', 'manager').order_by('-create_time', '-id')
        paginator = self.pagination(vo.all(), number_per_page=args.pageSize)
        return self.render_json(data=paginator)


class VideoOrderDetailController(BaseController):

    def get(self):
        """视频订单详情
        """
        id = self.int_argument('id', 0)
        if id <= 0:
            return self.render_json(msg='参数错误', code=1001)
        vo = VideoOrder.objects.filter(id=id).first()
        if not vo:
            return self.render_json(msg='订单不存在', code=1001)

        return self.render_json(data=vo.to_detail_dict())



class ModifyOrder(BaseController):

    def update_team(self, team_id, state):
        if not team_id or int(team_id) <= 0:
            return None
        team = Team.objects.filter(id=team_id).first()
        if not team:
            return None
        team.state = state
        team.save()
        return team

    def query_video_order_state(self, state_id):
        if not state_id or int(state_id) <= 0:
            return None

    def post(self):
        """更新订单状态
        """
        data = json.loads(self.request.body)
        print('参数：{}'.format(data))
        video_order_id = data.get('id', '')
        if not video_order_id:
            return self.render_json(code=1010, msg='参数不能为空')
        vo = VideoOrder.objects.filter(id=video_order_id).first()
        if not vo:
            return self.render_json(code=1011, msg='视频订单不存在')
        has_change = False
        order_status = data.get('order_status', '')
        order_old_order_status = vo.order_status
        if order_status:
            

            order_status = int(order_status)
            vo.order_status = order_status
            has_change = True
        manager_id = data.get('manager_id', '')
        if manager_id:
            manager_id = int(manager_id)
            vo.manager_id = manager_id
            has_change = True

        price = data.get('price', '')
        if price:
            has_change = True
            vo.price = price
        if has_change:
            vo.save()
        print(order_old_order_status, order_old_order_status == VideoOrder.STATE_FINISH_PAY, order_status)
        # 外部支付确认
        if order_old_order_status == VideoOrder.STATE_FINISH_PAY and order_status == VideoOrder.STATE_PENDING_ORDERS:
            VideoOrder.objects.init_order_team_and_producer(vo.id)
            user = vo.user
            print(user)
            if user:
                user.send_sms_2_managers()
                user.send_sms_2_user()
        return self.render_json(data={})       